<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>出入记录</title>
    <link rel="stylesheet" href="css/bootstrap.css">
    <script src="js/jquery.js"></script>
    <script src="js/common.js" type="text/javascript"></script>
    <script src="js/jsrender.js"></script>
    <script src="js/bootstrap.js"></script>
    <script type="text/javascript">
        $(function () {
        	if(localStorage.getItem("token")==null){
                window.location.href="login.html"
            }
        	
        	if(localStorage.getItem("customerid")!=null){
        		cid = localStorage.getItem("customerid")
        		list()
        	}
        	else{
        		cid = 0
        		list()
        	}
        		
            //注册转换器
            $.views.converters("dateformat" , function (val) {
                var mydate = new Date(val)
                var y = mydate.getFullYear()
                var m = mydate.getMonth()+1
                var d = mydate.getDate()
                return y + "-" + (m < 10 ? "0" + m : m) + "-" + (d < 10 ? "0" + d : d)
            })
            //请求该客户所有出入记录
            function list(){
            	 $.ajax({
                     url:GC.bctx + "customers/getGoout/"+cid,//后端请求映射路径
                     type:"GET",
                     headers: {"Authorization": localStorage.getItem("token")},
                     success:function (r) {
                         if(r.successed){
                             //jsrender模板渲染
                             $("#gooutInfo").html($("#gooutInfoTemplate").render(r.data))
                         }else{
                             alert(r.message)
                         }
                     }
                 })
            }
           
            //确认归院
            $("#gooutInfo").on("click",".btn-delete",function () {
                var gooutdate = $(this).attr("data-gooutdate")
                var expectedbackdate = $(this).attr("data-expectedbackdate")
                $.ajax({
                    url:GC.bctx + "customers/getback",
                    type:"PUT",
                    dataType:"json",
                    data:{"gooutdate":gooutdate , "expectedbackdate":expectedbackdate},
                    headers: {"Authorization": localStorage.getItem("token")},
                    success:function(r){
                        if(r.successed){
                            //成功后
                            alert(r.message)
                            window.location.href="listGoout.html"
                        }else{
                            alert(r.message)
                        }
                    }
                })

            })
            //响应外出登记
            $("#new").click(function() {
                window.location.href="goout.html"
            })
            
        })
    </script>
    <script id="gooutInfoTemplate" type="text/x-jsrender">
	<tr align="center">
		<td>{{:customerid}}</td>
		<td>{{:gooutdate}}</td>
		<td>{{:expectedbackdate}}</td>
		<td>{{:actuallybackdate}}</td>
		<td>{{:gooutreason}}</td>
		<td colspan="2" align="center">
            <button id="checkGoInfo" class="btn btn-delete btn-info" 
	data-gooutdate="{{:gooutdate}}" data-expectedbackdate="{{:expectedbackdate}}">确认返院</button>
         </td>
	</tr>
    </script>
</head>
<body>
<button id="new" class="btn btn-primary">新增外出登记</button>
<table class="table table-bordered"">
    <tr>
    	<th>客户id</th>
        <th>外出日期</th>
        <th>预期返回日期</th>
        <th>实际返回日期</th>
        <th>外出原因</th>
        <th>确认返院</th>
    </tr>
    <tbody id="gooutInfo"></tbody>
</table>
</body>
</html>